// pkgArticle/pages/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cateList: [],
    articleList: [],
    queryParams: {
      page: 1,
      limit: 10,
      cateid: 1
    },
    totalPages: 0,
    activeId: 3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取路由中的cateid
    this.data.queryParams.cateid = +options.id
    this.setData({ activeId: +options.id })
    this.getCateList()
    this.getArticleList()
  },
  async getCateList() {
    const res = await wx.http.get('/index/index')
    this.setData({ cateList: res.data.allCate })
  },
  async getArticleList() {
    const { data: { list } } = await wx.http.get('/index/list', this.data.queryParams)
    this.data.articleList.push(...list.data)
    this.setData({ articleList: this.data.articleList, totalPages: list.totalPages })
  },
  // 切换tab
  tabChange(ev) {
    this.data.articleList = []
    this.data.queryParams.cateid = ev.detail.name
    this.getArticleList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.queryParams.page >= this.data.totalPages) return
    this.data.queryParams.page++
    this.getArticleList()
  },

})